<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('修改商家店铺')"/>
    <th:block th:include="include :: bootstrap-fileinput-css"/>
    <th:block th:include="include :: summernote-css"/>
    <link th:href="@{/ajax/libs/select2/select2.min.css}" rel="stylesheet"/>
    <link th:href="@{/ajax/libs/select2/select2-bootstrap.css}" rel="stylesheet"/>
</head>
<style>
    .submited {
        float: right;
        width: 120px;
        height: 40px;
        background: linear-gradient(#ff0000, #ff1155);
        border: 0;
        border-radius: 5px;
        color: #ffff;
        font-size: 18px;
        margin-right: 9%;
    }

    .smimg img {width: 20px;height: 20px;}

</style>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-WxShop-edit" th:object="${wxShop}">
        <input name="id" th:field="*{id}" type="hidden">
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">商铺名称：</label>
                    <div class="col-sm-8">
                        <input name="shopName" th:field="*{shopName}" class="form-control" type="text" required
                               disabled="disabled">
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">管理员电话：</label>
                    <div class="col-sm-8">
                        <input name="shopTel" th:field="*{shopTel}" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">商铺分类：</label>
                    <div class="col-sm-8">
                        <select name="shopTypeId" id="shopTypeId" th:with="type=${@dict.selectShopTypeList()}"
                                class="form-control m-b" required>
                            <option th:each="dict : ${type}" th:text="${dict.typeName}" th:value="${dict.id}"
                                    th:field="*{shopTypeId}"></option>
                        </select>

                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">店铺地址：</label>
                    <div class="col-sm-8">
                        <input name="shopAdress" th:field="*{shopAdress}" class="form-control" type="text" required onclick="selectMap()">
                        <input  th:field="*{shopLatitude}" type="text" hidden>
                        <input  th:field="*{shopLongitude}"  type="text" hidden>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">当前让利比：</label>
                    <div class="col-sm-8">

                        <select name="discountRatio" th:field="*{discountRatio}" class="form-control m-b"
                                disabled="disabled" required>
                            <option value="0.02">2%</option>
                            <option value="0.03">3%</option>
                            <option value="0.04">4%</option>
                            <option value="0.05">5%</option>
                            <option value="0.06">6%</option>
                            <option value="0.07">7%</option>
                            <option value="0.08">8%</option>
                            <option value="0.09">9%</option>
                            <option value="0.1">10%</option>
                            <option value="0.11">11%</option>
                            <option value="0.12">12%</option>
                            <option value="0.13">13%</option>
                            <option value="0.14">14%</option>
                            <option value="0.15">15%</option>
                        </select>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">关键字绑定：</label>
                    <div class="col-sm-8">
                        <select class="form-control select2-multiple" multiple id="wordsIds"  required>
                            <option th:each="words:${wordsList}" th:value="${words.id}" th:text="${words.importWords}"
                                    th:selected="${words.flag}"></option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">商铺展示电话：</label>
                    <div class="col-sm-8">
                        <input name="sysTel" th:field="*{sysTel}" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
        </div>

      <!--  <div class="row">
            <div class="col-sm-6">
                <div class="form-group" style="display: flex;align-items: center;">
                    <label class="col-sm-4 control-label smimg" style="padding-top: 0;color: #0d8ddb;"><img
                            th:src="@{/shuoming.png}" alt=""> 说明：</label>
                    <div class="col-sm-8" style="color: #0d8ddb;">
                        每次更改让利比后，次日零点开始生效
                    </div>
                </div>
            </div>
        </div>-->
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-4 control-label ">商铺公告：</label>
                    <div class="col-sm-8">
                        <input name="shopMsg" th:field="*{shopMsg}" class="form-control" type="text" maxlength="22">
                    </div>
                </div>
            </div>
            <div class="col-sm-6" >
                <div class="form-group">
                    <label class="col-sm-4 control-label ">
                        <button type="button" class="btn btn-sm btn-warning" onclick="toLookShopImg()"><i
                                class="fa fa-search"></i>查看商铺二维码
                        </button>
                    </label>
                    <input type="text" th:field="*{shopImg}" id="shopImg" hidden>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">店铺简介：</label>
            <div class="col-sm-8">
                <input name="shopDetailImg" th:field="*{shopDetailImg}" type="text" id="shopDetailImg" hidden>
                <div class="summernote" id="editor"></div>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">主图：</label>
            <input name="logoImg" th:each="img : *{logoImgs}" th:value="${img.imgPath}" class="form-control hidden"
                   type="text">
            <div class="col-sm-8">
                <input class="file" id="showLogoImg" type="file"  accept=".jpg,.img,.png" data-theme="fas">
            </div>
        </div>
    </form>
    <button onclick="submitHandler()" class="submited"> 保存</button>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: bootstrap-fileinput-js"/>
<th:block th:include="include :: summernote-js"/>
<script th:src="@{/ajax/libs/select2/select2.min.js}"></script>
<script type="text/javascript">
    var prefix = ctx + "WxShop/WxShop";
    $("#form-WxShop-edit").validate({
        focusCleanup: true
    });

    $(function () {
        $('.summernote').summernote({
            height: 192,
            lang: 'zh-CN',
            followingToolbar: false,
            callbacks: {
                onChange: function (contents, $edittable) {
                    $("input[name='" + this.id + "']").val(contents);
                },
                onImageUpload: function (files) {
                    var obj = this;
                    var data = new FormData();
                    data.append("file", files[0]);
                    $.ajax({
                        type: "post",
                        url: ctx + "common/upload",
                        data: data,
                        cache: false,
                        contentType: false,
                        processData: false,
                        dataType: 'json',
                        success: function (result) {
                            if (result.code == web_status.SUCCESS) {
                                $('#' + obj.id).summernote('insertImage', result.url);
                            } else {
                                $.modal.alertError(result.msg);
                            }
                        },
                        error: function (error) {
                            $.modal.alertWarning("图片上传失败。");
                        }
                    });
                },
            }
        });

        $("#wordsIds").select2({
            minimumInputLength:0,
            maximumSelectionLength:5
        })

    });


    //logo
    $(function () {
        var logoImgs = $("input[name='logoImg']");
        var logoArray = []
        for (var x = 0; x < logoImgs.length; x++) {
            logoArray.push(ctx + logoImgs[x].defaultValue);
        }
        $("#showLogoImg").fileinput('destroy');
        $("#showLogoImg").fileinput({
            language: 'zh', //设置语言
            uploadUrl: "upload/insert", //上传的地址
            maxFileSize: 1024,//文件大小限制
            uploadAsync: false, //默认异步上传
            showUpload: false, //是否显示上传按钮
            showRemove: true, //显示移除按钮
            showPreview: true, //是否显示预览
            showCaption: true,//是否显示标题
            browseClass: "btn btn-primary", //按钮样式
            initialPreviewAsData: true,
            initialPreviewFileType: 'image',
            initialPreview: logoArray,
            enctype: 'multipart/form-data',
            validateInitialCount: true
        });
        //删除无用按钮
        $(".file-actions").remove();

        var content = $("#shopDetailImg").val();
        $('#editor').summernote('code', content);
        var cityId = $('#treeId').val()
        if (cityId) {
            $.get(ctx + "WxCitys/WxCitys/selectOneCity/" + cityId, function (res) {
                $("#treeName").val(res)
            })
        }
    })


    function submitHandler() {
        if ($.validate.form()) {
            var sHTML = $('.summernote').summernote('code');
            $("#shopDetailImg").val(sHTML);
            var formData = new FormData($("#form-WxShop-edit")[0]);
            if ($('#showLogoImg')[0].files[0] != null) {
                formData.append('logoImgFiles', $('#showLogoImg')[0].files[0]);
            }
            formData.append('wordsIds', $("#wordsIds").val().toString())
            $.ajax({
                url: prefix + "/editShop",
                type: 'post',
                cache: false,
                data: formData,
                processData: false,
                contentType: false,
                dataType: "json",
                success: function (result) {
                    $.operate.successCallback(result);
                }
            });
        }
    }

    function toLookShopImg() {
        var shopImg = $("#shopImg").val()
        layer.open({
            title: '商铺二维码',
            offset: '100px',
            content: ' <img src="' + ctx + shopImg + '"  alt="">'
        });
    }
    
    function selectMap() {
        var url=prefix+"/showMapinfo";
        $.modal.open("选择地址",url);
    }
</script>
</body>
</html>